package top.braycep.ui;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import top.braycep.bean.JarFile;
import top.braycep.bean.JarInnerFile;
import top.braycep.bean.Setting;
import top.braycep.db.DbUtils;
import top.braycep.exception.BaseException;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.List;
import java.util.Vector;

public class UIUtils {
    public static void checkNameAndPath(JTextField txtFileName, JTextField txtFilePaht) {
        if (StrUtil.isEmpty(txtFileName.getText())) {
            throw new BaseException("\u6587\u4EF6\u540D\u4E3A\u7A7A\uFF0C\u8BF7\u68C0\u67E5");
        }

        if (StrUtil.isEmpty(txtFilePaht.getText())) {
            throw new BaseException("\u6587\u4EF6\u8DEF\u5F84\u4E3A\u7A7A\uFF0C\u8BF7\u68C0\u67E5");
        }
    }

    public static void checkTable(JTable table) {
        if (table.getRowCount() == 0) {
            throw new BaseException("\u8868\u683C\u4E3A\u7A7A\uFF0C\u8BF7\u68C0\u67E5");
        }
    }

    public static void showInfo(JFrame frame, String msg) {
        JOptionPane.showMessageDialog(frame, msg, "\u63D0\u793A", JOptionPane.INFORMATION_MESSAGE);
    }

    public static JarFile buildJarFile(Long id, String filename, String filepath, DefaultTableModel model) {
        JarFile jarFile = new JarFile(filename, filepath);
        jarFile.setId(id);
        jarFile.setIsDefault(false);

        Vector dataVector = model.getDataVector();
        for (int i = 0; i < dataVector.size(); i++) {
            Vector data = (Vector) dataVector.get(i);
            if (data != null) {
                Object idObj = data.get(0);
                Long innerId = null;
                if (idObj != null && !StrUtil.isEmpty(idObj.toString())) {
                    innerId = Long.parseLong(idObj.toString());
                }
                String realpath = data.get(1) != null ? data.get(1).toString() : "";
                String mappedPath = data.get(2) != null ? data.get(2).toString() : "";
                String rule = data.get(3) != null ? data.get(3).toString() : "";
                if (!StrUtil.isEmpty(realpath)) {
                    JarInnerFile innerFile = new JarInnerFile(realpath);
                    innerFile.setId(innerId);
                    innerFile.setMappedpath(mappedPath);
                    innerFile.setRule(rule);
                    innerFile.setJarId(id);
                    innerFile.setJarFile(jarFile);
                    jarFile.getInnerFiles().add(innerFile);
                }
            }
        }

        return jarFile;
    }

    public static void loadData(MainUI mainUI, JarFile jarFile) {
        if (jarFile == null) {
            jarFile = DbUtils.findDefault();
        }
        if (jarFile != null) {
            loadConfig(mainUI, jarFile);
        }

        List<JarFile> jarFiles = DbUtils.findAllConfig();
        if (jarFiles != null) {
            refreshJarTable(mainUI, jarFiles);
        }

        loadSettings(mainUI);
    }

    public static void refreshJarTable(MainUI mainUI, List<JarFile> jarFiles) {
        clearTable(mainUI.getTblJars());
        for (JarFile file : jarFiles) {
            DefaultTableModel model = (DefaultTableModel) mainUI.getTblJars().getModel();
            model.addRow(new Object[]{file.getId(), file.getFilename(), file.getFilepath(), file.getIsDefault()});
        }
    }

    public static void loadConfig(MainUI mainUI, JarFile defaultValue) {
        if (defaultValue != null) {
            mainUI.getTxtId().setText(StrUtil.toString(defaultValue.getId()));
            mainUI.getTxtFileName().setText(defaultValue.getFilename());
            mainUI.getTxtFilePath().setText(defaultValue.getFilepath());

            clearTable(mainUI.getTblFiles());
            DefaultTableModel model = (DefaultTableModel) mainUI.getTblFiles().getModel();
            CollectionUtil.sortByProperty(defaultValue.getInnerFiles(), "id");
            for (JarInnerFile innerFile : defaultValue.getInnerFiles()) {
                model.addRow(new Object[]{innerFile.getId(), innerFile.getRealpath(), innerFile.getMappedpath(), innerFile.getRule()});
            }
        }
    }

    public static void clearTable(JTable table) {
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        for (int i = model.getRowCount() - 1; i >= 0; i--) {
            model.removeRow(i);
        }
    }

    public static void loadSettings(MainUI mainUI) {
        List<Setting> settings = DbUtils.findAllSettings();

        if (settings != null && settings.size() > 0) {
            JTable tblSettings = mainUI.getTblSettings();

            clearTable(tblSettings);
            DefaultTableModel model = (DefaultTableModel) tblSettings.getModel();
            for (Setting setting : settings) {
                model.addRow(new Object[]{setting.getId(), setting.getName(), setting.getValue(), setting.getDesc()});
            }
        }
    }

    public static void updateSetting(MainUI ui, JTable tblSettings) {
        int row = tblSettings.getSelectedRow();
        DefaultTableModel model = (DefaultTableModel) tblSettings.getModel();
        Vector cols = (Vector) model.getDataVector().get(row);
        if (cols != null && cols.size() > 0) {
            Setting setting = new Setting();
            setting.setId((Long) cols.get(0));
            setting.setName((String) cols.get(1));
            setting.setValue((String) cols.get(2));
            setting.setDesc((String) cols.get(3));

            DbUtils.updateSetting(setting);
        }
    }
}
